package com.sihai.ehr;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: wxn
 * since: 12-6-29 Time: 上午10:35
 */
public class PoiDemo {

    public void read() throws Exception {
        FileInputStream in = new FileInputStream(new File("/home/wxn/test.doc"));
        WordExtractor extrator = new WordExtractor(in);
        String content = extrator.getText();
        System.out.println(content);
    }

    public void editDocTemplate(Map<String, String> map) throws Exception {
        FileInputStream in = new FileInputStream(new File("/home/wxn/test.doc"));
        HWPFDocument document = new HWPFDocument(in);

        System.out.println("*********");
        System.out.println(document.getDocumentText());
        System.out.println("-----");

        //替换读取到的word模板内容的指定字段
        Range range = document.getRange();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            range.replaceText(entry.getKey(), entry.getValue());
            String temp = range.text();
            System.out.println(temp);
        }
        String text = document.getDocumentText();
        System.out.println(text);


        //-----------------写文件
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        String fileName = ""+System.currentTimeMillis();
        fileName += ".doc";
        FileOutputStream out = new FileOutputStream("/home/wxn/test2.doc",true);
        document.write(ostream);
        //输出字节流
        out.write(ostream.toByteArray());
        out.close();
        ostream.close();

    }

    //poi读excel
    private void readExcel(File file) {
        POIFSFileSystem fs = null;
        HSSFWorkbook wb = null;
        HSSFSheet hssfSheet = null;
        try {
            fs = new POIFSFileSystem(new FileInputStream(file));
            wb = new HSSFWorkbook(fs);

            hssfSheet = wb.getSheetAt(0);
            // 得到总行数
            int rowNum = hssfSheet.getLastRowNum();

            List<String[]> list = new ArrayList<String[]>();

            HSSFRow row = hssfSheet.getRow(0);
            //int colNum = row.getPhysicalNumberOfCells();
            int colNum = 78;
            DecimalFormat decimalFormat = new DecimalFormat("#");
            for (int i = 3; i <= rowNum; i++) {
                row = hssfSheet.getRow(i);
                int j = 0;
                String[] data = new String[colNum];
                while (j < colNum) {
                    if(row.getCell(j) == null){
                        data[i] = "";
                    }else{
                        switch (row.getCell(j).getCellType()){
                            case HSSFCell.CELL_TYPE_NUMERIC:
                                data[j] = decimalFormat.format(row.getCell(j).getNumericCellValue());
                                break;
                            case HSSFCell.CELL_TYPE_STRING:
                                data[j] = row.getCell(j).getStringCellValue().trim();
                                break;
                            default:
                                data[j] = "";
                                break;
                        }
                    }
                    //System.out.print(data[j] + " ");
                    j++;
                }
                list.add(data);
                //System.out.println("");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //poi写excel
    private void createExcelFromTemplate() {
        HSSFWorkbook workBook = null;
        try {
            //读取模板Excel
            workBook = new HSSFWorkbook(new FileInputStream("/home/wxn/public/test.xls"));
            //workBook.createSheet("employee_info").createRow(30).createCell(2, Cell.CELL_TYPE_STRING).setCellValue("anle102你好");
            workBook.getSheet("employee_info").getRow(10).createCell(60, Cell.CELL_TYPE_STRING).setCellValue("anle102你好");
            FileOutputStream out = new FileOutputStream("/home/wxn/public/test22.xls");
            // 把相应的Excel 工作簿存盘
            workBook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {
        PoiDemo poiDemo = new PoiDemo();
        //poiDemo.read();

        System.out.println("-------------------");

        Map<String, String> map = new HashMap<String, String>();
        map.put("ehr_name", "汪小伦");
        map.put("ehr_sex", "男");
        map.put("ehr_age", "18");
        //poiDemo.editDocTemplate(map);

        //poiDemo.createExcelFromTemplate();
        poiDemo.readExcel(new File("/home/wxn/public/test.xls"));
    }
}
